package com.mls.guardian.container;


import com.mls.guardian.config.AppSTConfig;
import com.mls.guardian.config.MasterConfig;
import com.mls.guardian.config.NettyConfig;
import com.mls.guardian.config.SlaveConfig;
import com.mls.guardian.em.ContainerRole;
import com.mls.guardian.executor.slave.*;
import com.mls.guardian.project.ProjectConfigFactory;
import com.mls.guardian.protocol.CodeType;
import com.mls.guardian.remoting.client.Client;
import com.mls.guardian.remoting.netty.NettyClient;
import com.mls.guardian.remoting.netty.RemotInvoke;
import com.mls.guardian.service.HeartBeat2MService;

/**
 * Created by jl on 15/9/9.
 */
public class SlaveContainer extends BaseContainer {
    //netty客户端
    RemotInvoke remotInvoke;
    protected SlaveConfig slaveConfig = new SlaveConfig();
    private Client keepaliveMasterClient;
    private HeartBeat2MService heartBeat2MService;


    public SlaveContainer(MasterConfig masterConfig, SlaveConfig slaveConfig
            , AppSTConfig appStConfig, NettyConfig nettyConfig) throws Exception {
        super(appStConfig);
        this.slaveConfig = slaveConfig;
        this.keepaliveMasterClient = new NettyClient(slaveConfig.getMasterIp(), slaveConfig.getMasterPort(), nettyConfig);
        heartBeat2MService = new HeartBeat2MService(keepaliveMasterClient, this);
        this.role = ContainerRole.SLAVE;
    }


    @Override
    public void startup() throws Exception {
        projectObjList = ProjectConfigFactory.createProjects();

        this.getAppController().setProjectObjList(projectObjList);

        keepaliveMasterClient.registerExcutor(CodeType.START, new SlaveStartAppExecutor(this));
        keepaliveMasterClient.registerExcutor(CodeType.RESTART, new SlaveRestartAppExecutor(this));
        keepaliveMasterClient.registerExcutor(CodeType.STOP, new SlaveStopAppExecutor(this));
        keepaliveMasterClient.registerExcutor(CodeType.GET_BACKUP, new SlaveGetBackupExecutor(this));
        keepaliveMasterClient.registerExcutor(CodeType.UPDATE_STATIC_FILE, new SlaveUpdateStaticFileExecutor(this));
        keepaliveMasterClient.registerExcutor(CodeType.RESTORE, new SlaveRestoreAppExecutor(this));
        keepaliveMasterClient.registerExcutor(CodeType.SERVER_PROCESS_INFO, new SlaveServerProcessInfoExecutor(this));
        keepaliveMasterClient.registerExcutor(CodeType.GET_FILE_INFO, new SlaveGetFileInfoExecutor(this));
        keepaliveMasterClient.registerExcutor(CodeType.UPGRADE, new SlaveUpgradeExecutor(this));
        keepaliveMasterClient.registerExcutor(CodeType.GET_UPGRADE_VERSION, new SlaveGetUpgradeVersionExcutor(this));

        appController.startup(projectObjList);
        heartBeat2MService.start();
    }


    @Override
    public void shutdown() {
        heartBeat2MService.shutdown();
        keepaliveMasterClient.shutdown();

    }

}
